
package estructuras;

public class CDL {
    
    private CDLNode _head;
    private CDLNode _tail;
    private int _length = 0;
    
    private CDL (){
        _head = null;
        _tail = null;
    }
    
    private CDLNode crearNodo(Object pPorCrear){
        CDLNode temp = new CDLNode (pPorCrear);
        return temp;
    }
    
    private void meterHead(Object pPorMeter){
        CDLNode temp = crearNodo(pPorMeter);
        
        _head = _tail = temp;
        _tail._next = _head;
        _head._prew = _tail;
    }
    
    private void meterEnPosicion (Object pPorMeter, int pPosicion){
        CDLNode temp = crearNodo(pPorMeter);
        
        if(_head == null){
            meterHead(pPorMeter);  // no importa la posicion que quiera primero el head
        }
        else if (pPosicion >= _length-1) {
            _tail._next = temp;            
            temp._prew = _tail;
            temp._next =  _head;
            _tail = temp;
            _head._prew = _tail; 
        } 
        else if (pPosicion == 0){
            temp._next = _head;
            _head._prew = temp;
            temp._prew = _tail;
            _tail._next = temp;
            _head = temp;            
        }
        else{
            int contador = 0;
            CDLNode temp2 = _head;
            while (contador +1 != pPosicion){
                temp2 = temp2._next;
                contador++;
            }
            temp._next = temp2._next;
            temp._prew = temp2;
            temp2._next._prew = temp;
            temp2._next = temp;
        }    
        _length++;
    }
    
    private void imprimir(){
        CDLNode aux = _head;
        System.out.print(aux._elemento + ", ");
        
        for(aux = aux._next; aux != _head;aux = aux._next){
            System.out.print(aux._elemento+ ", ");            
        }
        System.out.println("");
        System.out.println("tamaño_lista = "+_length);
    }
    
//    public static void main(String[] args) {
//        CDL listaCircular = new CDL ();
//        
//        listaCircular.meterEnPosicion(12, 1);
//        listaCircular.meterEnPosicion(15, 2);
//        listaCircular.meterEnPosicion(13, 3);
//        listaCircular.meterEnPosicion(66, 0);
//        listaCircular.meterEnPosicion(176, 4);
//        listaCircular.meterEnPosicion(187, 0);
//        listaCircular.meterEnPosicion(187, 1234567);        
//        
//        listaCircular.imprimir();        
//    }
}
